<?php
namespace app\index\util;

use think\Controller;

//返回数据基础类
class Response extends Controller
{

    private $code;
    private $message;
    private $data;
    private $StackTrace;
    private $type;
    private $count = 0;
    
    public function  __construct()
    {
        parent::__construct();
        $this->code = 200;
        $this->message = '';
        $this->data = new \stdClass();
        $this->StackTrace = '';
        $this->type = '';
    }

    public function __get($property_name)
    {
        if (isset($this->$property_name)) {
            return($this->$property_name);
        } else {
            return null;
        }
    }
    
    public function __set($property_name, $value)
    {
        $this->$property_name = $value;
    }
    public function getData()
    {
        $data = $this->data;
       
        $data = json_encode($data);
        //处理id
        $id = '"id":';
        $newId = '"Id":';
        $data = str_replace($id, $newId, $data);

        $data = json_decode($data, true);
        //处理json
        if (is_array($data)) {
            array_walk_recursive($data, function (&$var, $key) {
                if (!is_array($var)) {
                    $type = gettype($var);
                    if (json_decode($var) !== null) {
                        
                        $var = json_decode($var, true);
                        if ($type === 'string' && !is_array($var)) {
                            $var = (string)$var;
                        }
                        if ($type === 'boolean'  && !is_array($var)) {
                            $var = (boolean)$var;
                        }
                    }
                    if ($var === null) {
                        $var = "";
                    }
                }
            });
        }
        if ($this->type === 'array') {
            $data = ($data != '') ? $data : [];
        } else {
            $data = ($data != '' and $data != []) ? $data : new \stdClass();
        }
        return [
            'code' => $this->code,
            'message' => $this->message,
            'data' => $data,
            'StackTrace' => $this->StackTrace,
        ];
    }    
    function is_json($string) {
        json_decode($string);
        return (json_last_error() == JSON_ERROR_NONE);
    }

    //处理空字符串
    private function _unsetNull(&$arr)
    {
        if ($arr !== null) {
            if (is_array($arr)) {
                if (!empty($arr)) {
                    foreach ($arr as $key => &$value) {
                        if ($value === null) {
                            $arr[$key] = "";
                        } else {
                            
                            //处理数据库json字符串
                            if (!is_array($arr[$key])) {
                                if (json_decode($arr[$key]) !== null) {
                                    $arr[$key] = json_decode($arr[$key],true);
                                }
                            }
                            if ($key === 'id') { 
                                $arr['Id'] = (string)$arr[$key];
                                $key = 'Id';
                                unset($arr['id']);
                            }

                            if ($this->count >= 1000) {
                                return $arr;
                            }
                            $this->count ++;
                            $arr[$key] = $this->_unsetNull($value);      //递归再去执行
                        }
                    }
                } else { 
                    // $arr = "";
                }
            } else {
                if($arr === null){ $arr = ""; }         //注意三个等号
            }
        } else { $arr = ""; }
        return $arr;
    }
}